package com.hisense.hiioc.hismartplate.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther:liaohongbing@hisense.com
 * @date:2019/5/2 11:08
 * @des
 */
@Service
public class EnvironmentService {
    private static final String weatherUrl = "https://free-api.heweather.net/s6/weather?location=南明,贵阳&key=7fecc6f4be2448f3ba43c6ec73d5ae28";

    @Autowired
    RestTemplate restTemplate;


    public JSONArray getDailyForecast() {
        JSONArray forecastList = new JSONArray();
        try {
            forecastList = restTemplate.getForEntity(weatherUrl, JSONObject.class).getBody()
            .getJSONArray("HeWeather6").getJSONObject(0).getJSONArray("daily_forecast");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return forecastList;
    }

    public JSONArray getLifestyle(){
        JSONArray lifeList = new JSONArray();
        try {
            lifeList = restTemplate.getForEntity(weatherUrl,JSONObject.class).getBody().
                    getJSONArray("HeWeather6").getJSONObject(0).getJSONArray("lifestyle");
        }catch (Exception e ){
            e.printStackTrace();
        }
        return lifeList;

    }

    public JSONObject getEnvironmentInfo(){

        JSONObject res = new JSONObject();
        JSONArray forecastList = new JSONArray();
        try {
            forecastList = restTemplate.getForEntity(weatherUrl, JSONObject.class).getBody()
                    .getJSONArray("HeWeather6").getJSONObject(0).getJSONArray("daily_forecast");
        } catch (Exception e) {
            e.printStackTrace();
        }

        JSONArray lifeList = new JSONArray();
        try {
            lifeList = restTemplate.getForEntity(weatherUrl,JSONObject.class).getBody().
                    getJSONArray("HeWeather6").getJSONObject(0).getJSONArray("lifestyle");
        }catch (Exception e ){
            e.printStackTrace();
        }
        res.put("forecast",forecastList);
        res.put("lifeStyle",lifeList);
        return res;

    }



}
